# -*- coding: utf-8 -*-
# @Time   : 2023/9/15 10:30
# @Author : 大龙 🚀
# @File   : dalian_illegal.py
import re

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider


class DaLianIllegalSpider(BaseTaxIllegalSpider):
    name: str = "dalian_illegal"
    province: str = "大连"
    url: str = "http://dalian.chinatax.gov.cn/module/jslib/bulletin2/lpindex.html"  # 取首页

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.dalian.chinatax.gov.cn:8443"})
        url = "https://dalian.chinatax.gov.cn/module/jslib/bulletin2/lpajaxfors.jsp"
        data = {
            "tablename": "jcms_13",
            "na_name": "",
            "na_number": "",
            "na_address": "",
            "na_represent": "",
            "na_responsible": "",
        }
        yield self.FormRequest(url, formdata=data, callback=self.parse)

    def parse(self, response, **kwargs):
        url_list = response.xpath("//a/@href").getall()
        for url in url_list:
            yield response.follow(url, callback=self.parse_detail)

    def parse_detail(self, response, **kwargs):
        item = NetTaxIllegalProItem()
        info = []
        for i in response.xpath("//table//tr"):
            res = i.re(r"<td align=\"center\"[\w\W]*?>(.*?)\s*</td>\s*<td align=\"left\"[\w\W]*?>(?:\s*<.*?>)*\s*([^>]*?)(?:<.*?>)*\s*</td>")
            if res:
                info.append(res)
        info = dict(info)

        pub_date = response.xpath('//*[@name="pubdate"]/@content').get()
        item.year = pub_date[:10].replace("-", "")
        item.province = self.province
        name = sex = c_name = id_card = ""
        for k, v in info.items():
            if "纳税人（法人、其他组织或者自然人）名称" in k:
                item.company_name = v
            if "统一社会信用代码（纳税人识别号）" in k:
                item.taxpayer_id = v
            if "组织机构代码" in k:
                item.org_code = v
            if "注册地址" in k:
                item.city = item.resolve_register_address(v)[0]
                item.country = item.resolve_register_address(v)[1]
                item.address = v
            if "法定代表人或负责人或经法院生效裁判确定的实际责任人姓名" in k:
                name = v
            if "法定代表人、负责人、实际责任人性别" in k:
                sex = v
            if "法定代表人、负责人、实际责任人证件名称" in k:
                c_name = v
            if "法定代表人、负责人、实际责任人证件号码" in k:
                id_card = v
            if "案件性质" in k:
                item.illegal_status = v
            if "主要违法事实" in k:
                illegal_facts_src = re.sub(r"(<.*?>|\s*|“|”)", "", re.sub(r"(<!--.*?-->)", "", v))
                item.illegal_facts = illegal_facts_src
            if "相关法律依据及税务处理处罚情况" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.basis_and_punishment = v
            item.legal_representative = ",".join([name, sex, c_name, id_card])

        yield item


if __name__ == "__main__":
    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "dalian_illegal"])
